<?php

/**
 * System configuration module
 *
 * @author
 */

class Config extends _Module
{
    private static $instance;

    private $dependencies = array();

    private $configFile;

    private $configuration;

    private $type;

    private $defaultConfig;

    private function __construct()
    {
    }

    private function __clone()
    {
    }

    public static function &getInstance()
    {
        if (is_null(self::$instance)) {
            self::$instance = new self;
        }
        return self::$instance;
    }

    public function initialize()
    {
        $path = CONFIGS . '/config.xml';
        if (file_exists($path)) $this->configFile = $path;

        $this->loadConfigFile();
    }

    public function setType($type)
    {
        foreach($this->configuration as $key => $config)
        {
            if(strpos($type, (string)$config->general->url) !== false)
            {
                $this->defaultConfig = $key;
                break;
            }
        }

        if($this->defaultConfig)
        {
            $this->reInitConfigs();

            return (string)$this->configuration->{$this->defaultConfig}->general->url;
        }
    }

    public function getDependencies()
    {
        return $this->dependencies;
    }

    public function getDBHost()
    {
        return $this->configuration->{$this->defaultConfig}->database->host;
    }

    public function getDBUser()
    {
        return $this->configuration->{$this->defaultConfig}->database->user;
    }

    public function getDBPassword()
    {
        return $this->configuration->{$this->defaultConfig}->database->password;
    }

    public function getDBName()
    {
        return $this->configuration->{$this->defaultConfig}->database->name;
    }

    private function loadConfigFile()
    {
        if ($this->configFile) {
            $this->configuration = simplexml_load_file($this->configFile);
        }
    }

    private function reInitConfigs()
    {
        define('LIVE', $this->configuration->{$this->defaultConfig}->general->live);

        define('URL', substr($this->configuration->{$this->defaultConfig}->general->url,0,-1));

        define('THEME_URL', URL .
                          '/themes/' .
                          $this->configuration->{$this->defaultConfig}->theme->name);
        define('CSS', THEME_URL . '/static/styles');
        define('JS', THEME_URL . '/static/scripts');
        define('IMG', THEME_URL . '/static/images');

        # Current theme path
        define('THEME', THEMES . '/' . $this->configuration->{$this->defaultConfig}->theme->name);

        # Theme's controllers directory
        define('CONTROLLERS', THEME . '/controllers');

        # Theme's models directory
        define('MODELS', THEME . '/models');

        # Theme's views directory
        define('VIEWS', THEME . '/views');

        # Theme's static files directory
        define('STATICS', THEME . '/static');

        # Theme's image files directory
        define('IMAGES', STATICS . '/images');

        # Theme's script files directory
        define('SCRIPTS', STATICS . '/scripts');

        # Theme's style files directory
        define('STYLES', STATICS . '/styles');

        # Theme's helper files directory
        define('HELPERS', THEME . '/helpers');

        # Processed files directory uri
        define('PROCESSED_URL', URL . '/processed');

        # Include theme constants file
        require_once(HELPERS . '/constants.php');
    }
}